Skip to content

Add client options, WARN/TRACE/FATAL log levels and enum parsers - #48

Open
DzmitryHumianiuk wants to merge 1 commit into
masterfrom
feat/client-options
Open

DzmitryHumianiuk wants to merge 1 commit into
masterfrom
feat/client-options

Conversation

@DzmitryHumianiuk

Copy link
Copy Markdown
Member

Summary

Part of #27 (slice L1). No dependencies. feat/http-retries (L4) and feat/config-loader (L5) are stacked on this branch.

NewClient and NewReportingClient get a variadic opts ...Option, so that later settings (retries in L4, batching in L3) don't need new constructors. This changes the constructor signatures, which is why it should land before the first v6 tag (#32).

Changes

  • pkg/gorp/options.go (new): Option, ResponseMiddleware, WithResponseMiddleware.
    • A response middleware is func(resp *http.Response, body []byte) error, the shape of openapi.ResponseMiddlewareFunction. One function serves the Resty-based methods and the generated client.
    • Middlewares are chained in the order they were added and run before the client's own error handling, which stays in place. The first error stops the chain and becomes the error of the call.
    • The generated client has a single ResponseMiddleware slot. configureOpenAPI puts the user's middlewares in front of whatever the configuration already has there, it never replaces it.
  • pkg/gorp/client.go, pkg/gorp/reporting_client.go: opts ...Option on both constructors.
  • pkg/gorp/model_api.go: LogLevelTrace, LogLevelWarn, LogLevelFatal next to the existing DEBUG/INFO/ERROR.
  • pkg/gorp/model_parse.go (new): ParseLaunchMode, ParseItemType, ParseStatus. They ignore case and surrounding spaces and return an error wrapping ErrUnknownValue that lists the accepted values.
  • README, AGENTS.md, the reportportal skill: the new option and helpers.

Only one option is added here on purpose. WithRetry comes with L4. I did not add request middlewares, a user agent or a timeout option, because nothing in #26/#27 needs them yet.

Behaviour change

  • Source compatible for ordinary calls: NewClient(u, auth) and NewReportingClient(host, project, auth) compile unchanged.

  • Not compatible for code that stores a constructor in a typed function variable or passes it as a function value. Checked with a probe file:

    cannot use NewClient (value of type func(u *url.URL, clientOption ClientOption, opts ...Option) *Client) as func(*url.URL, ClientOption) *Client value in variable declaration
    cannot use NewReportingClient (value of type func(host string, project string, clientOption ClientOption, opts ...Option) *ReportingClient) as func(string, string, ClientOption) *ReportingClient value in variable declaration
    
  • Without options nothing changes on the wire or in the errors.

  • With a response middleware, the Resty clients buffer response bodies (SetResponseBodyUnlimitedReads(true)), otherwise the middleware would get an empty body for a response that Resty decoded into a result.

Fit with #36 (fix/api-errors)

I read pkg/gorp/errors.go on origin/fix/api-errors; this PR does not depend on it. #36 assigns conf.ResponseMiddleware = OpenAPIErrorMiddleware in newAPIClient, the same place where this PR calls o.configureOpenAPI(conf), so the two conflict in pkg/gorp/client.go (and, for L4, in AGENTS.md).

Resolution: keep both lines, #36's first:

conf.ResponseMiddleware = OpenAPIErrorMiddleware
o.configureOpenAPI(conf)

The other order would silently drop the user's middlewares on the generated client. I did a trial merge of feat/http-retries (which contains this PR) with origin/fix/api-errors, resolved it this way, and go build ./... && go vet ./... && go test -race ./... passed. The Resty side needs nothing: both branches keep the name defaultHTTPErrorHandler.

Verification

On feat/client-options (cfb8ad0):

  • go build ./... && go vet ./...: ok
  • go test -race ./...: ok (internal/commands, pkg/gorp)
  • go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0 run ./...: 0 issues
  • No live check: nothing changes on the wire.

Tests (pkg/gorp/options_test.go, pkg/gorp/model_parse_test.go):

  • a middleware sees a non-empty body on a 2xx response and StartLaunch still decodes id;
  • on a 4xx the middleware runs and the caller still gets *HTTPError (Resty) or *openapi.GenericOpenAPIError (generated) with the body intact;
  • a middleware error stops the chain and replaces the client's own error, on both clients;
  • a middleware can fail a successful response;
  • nil options and nil middlewares are ignored;
  • an existing ResponseMiddleware of the generated configuration is kept and runs after the user's.

Investigation log

  1. ClientOption already exists in pkg/gorp/client.go as func() *http.Client (the authentication builder), so the new type is named Option.
  2. The generated client calls cfg.ResponseMiddleware for every response, before its own status check: pkg/openapi/api_launch.go:120-125. There is one slot (pkg/openapi/configuration.go:98).
  3. Resty v3.0.0-rc.4 (go.mod:11) runs every response middleware even after one failed and only collects the errors (client.go:2568-2573 in the module cache). So the user's middlewares and defaultHTTPErrorHandler are registered as one Resty middleware; otherwise "the first error wins" would not hold.
  4. Response.Bytes() is empty after auto-unmarshal unless unlimited reads are on (response.go:142-151), hence the buffering.
  5. Log levels: the server's LogLevel enum has ERROR, WARN, INFO, DEBUG, TRACE, FATAL, UNKNOWN and matches names ignoring case: service-api infrastructure/persistence/entity/enums/LogLevel.java:28-32,60-65 (develop, 017642e9).
  6. Parsers: item types from TestItemTypeEnum.java:25-39, statuses from StatusEnum.java:25-35 (IN_PROGRESS and TO_RUN are not for clients, so ParseStatus rejects them), modes from reporting/Mode.java:27. The values already in model_enums.go match.

Not verified

  • No run against a real server.
  • Whether callers outside this repository store the constructors in typed variables.

🤖 Generated with Claude Code

NewClient and NewReportingClient take variadic options after the
authentication option. WithResponseMiddleware adds a hook that is called
with every response and its body before the client's own error handling;
hooks are chained and the first error stops the chain. The same hook
serves the Resty-based methods and the generated client.

ParseLaunchMode, ParseItemType and ParseStatus convert user input to the
typed values.

Part of #27

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants